package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired(required = false)
    private SetmealService setmealService;
    @GetMapping("/page")
    public R<Page<SetmealDto>> selectAll(Integer page,Integer pageSize,String name){
        Page<SetmealDto> setmealDtoPage = setmealService.selectAll(name, page, pageSize);
        return R.success(setmealDtoPage);
    }

    @PostMapping()
    public R<String> add(@RequestBody SetmealDto setmealDto, HttpSession session){
        Long employee = (Long) session.getAttribute("employee");
        setmealDto.setUpdateUser(employee);
        setmealDto.setCreateUser(employee);

        Boolean flag = setmealService.addSetmeal(setmealDto);

        if (!flag){
            return R.error("添加失败");
        }

        return R.success("添加成功");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> findById(@PathVariable Long id){
        SetmealDto byId = setmealService.findById(id);
        return R.success(byId);
    }

    @PutMapping()
    public R<String> update(@RequestBody SetmealDto setmealDto){
        Boolean flag = setmealService.update(setmealDto);
        if (!flag){
            return R.error("修改失败");
        }

        return R.success("修改成功");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids){
        Boolean flag = setmealService.updateStatus(status, ids);
        if (!flag){
            return R.error("修改失败");
        }
        else{
            return R.success("修改成功");
        }
    }

    @DeleteMapping()
    public R<String> deleteByIds(Long[] ids){
        Boolean flag = setmealService.deleteByIds(ids);

        if (!flag){
            return R.error("删除失败");
        }

        return R.success("删除成功");
    }

    //展示套餐
    @GetMapping("/list")
    public R<List<SetmealDto>> findByCategory(Long categoryId,Integer status){
        List<SetmealDto> byCategoryIdAndStatus = setmealService.findByCategoryIdAndStatus(categoryId, status);
        return R.success(byCategoryIdAndStatus);
    }




}
